package com.linjuke.mayouting.web.modules.action.biz;

import java.util.Map;

import com.linjuke.mayouting.biz.ao.CatAO;
import com.linjuke.mayouting.web.common.BaseAction;
import com.linjuke.water.common.domains.CategoryDO;
import com.zjuh.splist.core.annotation.DefaultTarget;
import com.zjuh.splist.core.form.Form;
import com.zjuh.splist.core.form.fileupload.FileItem;
import com.zjuh.splist.web.RunData;
import com.zjuh.splist.web.TemplateContext;
import com.zjuh.sweet.lang.CollectionUtil;
import com.zjuh.sweet.result.Result;

public class CatAction extends BaseAction {
	
	private CatAO catAO;
	
	@DefaultTarget("biz/cat/create")
	public void doCreate(RunData rundata, TemplateContext templateContext) {
		if (!checkCsrf(templateContext, "createCat")) {
			return;
		}
		final Form form = rundata.getForm("cat.create");
		if (!form.validate()) {
			return;
		}
		form.holdValues();
		CategoryDO cat = new CategoryDO();
		form.apply(cat);
		
		Map<String, FileItem> fileItems = rundata.getFileItems();
		FileItem catIcon = null;
		if (!CollectionUtil.isEmpty(fileItems)) {
			catIcon = fileItems.get("icon");
		}
		
		Result result = catAO.create(cat, catIcon);
		if (result.isSuccess()) {
			this.sendToModule("baseModule", "biz/cat/list");
		} else {
			this.handleError(result, rundata, templateContext);
		}
	}

	@DefaultTarget("biz/cat/list")
	public void doDelete(RunData rundata, TemplateContext templateContext) {
		int id = rundata.getQueryString().getInt("id");
		Result result = catAO.delete(id);
		if (result.isSuccess()) {
			this.sendToModule("baseModule", "biz/cat/list");
		} else {
			this.handleError(result, rundata, templateContext);
		}
	}
	
	public void setCatAO(CatAO catAO) {
		this.catAO = catAO;
	}
	
}
